//
//  NaviController.m
//  FunnyProject
//
//  Created by LYL on 3/24/13.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "MSNaviController.h"
#import "MSNaviBar.h"

#define kNaviBarH 44.f

@interface NViewController ()
@property (retain, nonatomic) MSNaviController *navi;
@end

@implementation NViewController
@synthesize navi = _navi;

- (void)dealloc{
    [_navi release];    _navi = nil;
    [super dealloc];
}

@end

@implementation MSNaviController
@synthesize viewControllers = _viewControllers;
@synthesize topViewController = _topViewController;
@synthesize visibleViewController = _visibleViewController;

- (void)dealloc{
    [_navibar release];
    [_containerView release];
    [_viewControllers release];
    [_topViewController release];
    [_visibleViewController release];
    
    [super dealloc];
}

- (id)initWithRootViewController:(NViewController *)controller{
    self = [super init];
    if (self) {
        self.view.backgroundColor = [UIColor whiteColor];

        CGRect rect = self.view.bounds;
        _navibar = [[MSNaviBar alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, kNaviBarH)];
        [self.view addSubview:_navibar];
        
        rect = CGRectMake(0, kNaviBarH, rect.size.width, rect.size.height - kNaviBarH);
        _containerView = [[UIView alloc] initWithFrame:rect];
        [self.view addSubview:_containerView];

        self.viewControllers = [NSArray array];
        [self pushViewController:controller animated:NO];
    }
    return self;
}


- (void)pushViewController:(NViewController *)controller animated:(BOOL)animated{
    NSMutableArray *controllers = [NSMutableArray arrayWithArray:self.viewControllers];
    [controllers addObject:controller];
    self.viewControllers = controllers;
    controller.navi = self;
    
    UIView *theView = controller.view;
    CGRect rect = theView.frame;
    rect.origin.y = 0.f;
    
    float duration = 0.f;
    if (animated) {
        duration = .3f;
        rect.origin.x = _containerView.bounds.size.width;
    } else {
        rect.origin.x = 0.f;
    }
    theView.frame = rect;
    [_containerView addSubview:theView];
    [UIView beginAnimations:@"ContainerID" context:nil];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    rect.origin = CGPointMake(0, 0);
    theView.frame = rect;
    [UIView commitAnimations];
}

- (void)popViewControllerAnimated:(BOOL)animated{
    NViewController *controller = [self.viewControllers lastObject];

    UIView *theView = controller.view;
    CGRect rect = theView.frame;
    rect.origin.y = 0.f;
    
    float duration = 0.f;
    if (animated) {
        duration = .3f;
        rect.origin.x = _containerView.bounds.size.width;
    } else {
        rect.origin.x = 0.f;
    }
    theView.frame = rect;
    [_containerView addSubview:theView];
    [UIView beginAnimations:@"ContainerID" context:nil];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    rect.origin = CGPointMake(0, 0);
    theView.frame = rect;
    [UIView commitAnimations];
    
    NSMutableArray *controllers = [NSMutableArray arrayWithArray:self.viewControllers];
    [controllers removeLastObject];
    self.viewControllers = controllers;
}

- (void)popToRootViewControllerAnimated:(BOOL)animated{
    
}

- (void)setViewControllers:(NSArray *)controllers animated:(BOOL)animated{
    
}

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated{
    
}

#pragma mark - Animation callback
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
    if ([animationID isEqualToString:@"ContainerID"]) {
        
    }
}

@end
